package com.apexwear.apexutils.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.DragAndDropPermissions;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.EABleConnectState;
import com.apex.bluetooth.enumeration.TimeZone;
import com.apex.bluetooth.model.EABleSyncTime;
import com.apexwear.apexutils.R;
import com.apexwear.apexutils.api.OnPositiveClickListener;
import com.apexwear.apexutils.data.TimeData;
import com.apexwear.apexutils.databinding.ActivityTimeBinding;
import com.apexwear.apexutils.dialog.SelectDialog;
import com.apexwear.apexutils.dialog.SelectTimeDialog;
import com.apexwear.apexutils.utils.ShowToast;

import java.util.Calendar;

public class TimeActivity extends AppCompatActivity {
    ActivityTimeBinding dataBinding;
    String[] yearArray, monthArray, dayArray, hourArray, minuteArray, secondArray, systemArray, areaArray, currentHourArray, currentMinuteArray, modeArray;
    SelectTimeDialog yearDialog, monthDialog, dayDialog, hourDialog, minuteDialog, secondDialog;
    SelectDialog systemDialog, areaDialog, currentHourDialog, currentMinuteDialog, modeDialog;
    TimeData timeData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_time);
        dataBinding.tooBar.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(dataBinding.tooBar);
        getSupportActionBar().setTitle(null);
        dataBinding.tooBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        timeData = new ViewModelProvider(TimeActivity.this).get(TimeData.class);
        dataBinding.rowYear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (yearArray == null) {
                    int currentYear = Calendar.getInstance().get(Calendar.YEAR);
                    yearArray = new String[10];
                    for (int i = currentYear - 5; i < currentYear + 5; i++) {
                        yearArray[i - (currentYear - 5)] = i + "";
                    }
                }
                if (yearDialog == null) {
                    yearDialog = new SelectTimeDialog(TimeActivity.this, yearArray, getString(R.string.year), 0);
                    yearDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.year.setText(mac);

                        }
                    });
                }
                if (!yearDialog.isShowing()) {
                    yearDialog.show();
                }


            }
        });
        dataBinding.rowMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (monthArray == null) {
                    monthArray = getResources().getStringArray(R.array.month_array);
                }
                if (monthDialog == null) {
                    monthDialog = new SelectTimeDialog(TimeActivity.this, monthArray, getString(R.string.month), 1);
                    monthDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.month.setText(mac);
                        }
                    });
                }
                if (!monthDialog.isShowing()) {
                    monthDialog.show();
                }

            }
        });
        dataBinding.rowDay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dayArray == null) {
                    int maxDay = Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH);
                    dayArray = new String[maxDay];
                    for (int i = 0; i < maxDay; i++) {
                        dayArray[i] = (i + 1) + "";
                    }

                }
                if (dayDialog == null) {
                    dayDialog = new SelectTimeDialog(TimeActivity.this, dayArray, getString(R.string.day), 2);
                    dayDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.day.setText(mac);
                        }
                    });
                }
                if (!dayDialog.isShowing()) {
                    dayDialog.show();
                }

            }
        });
        dataBinding.rowHour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (hourArray == null) {
                    hourArray = getResources().getStringArray(R.array.hour_array);
                }
                if (hourDialog == null) {
                    hourDialog = new SelectTimeDialog(TimeActivity.this, hourArray, getString(R.string.hour), 3);
                    hourDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.hour.setText(mac);
                        }
                    });
                }
                if (!hourDialog.isShowing()) {
                    hourDialog.show();
                }
            }
        });
        dataBinding.rowMinute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (minuteArray == null) {
                    minuteArray = new String[60];
                    for (int i = 0; i < 60; i++) {
                        minuteArray[i] = i + "";
                    }
                }
                if (minuteDialog == null) {
                    minuteDialog = new SelectTimeDialog(TimeActivity.this, minuteArray, getString(R.string.minute), 4);
                    minuteDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.minute.setText(mac);
                        }
                    });
                }
                if (!minuteDialog.isShowing()) {
                    minuteDialog.show();
                }

            }
        });
        dataBinding.rowSecond.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (secondArray == null) {
                    secondArray = new String[60];
                    for (int i = 0; i < 60; i++) {
                        secondArray[i] = i + "";
                    }
                }
                if (secondDialog == null) {
                    secondDialog = new SelectTimeDialog(TimeActivity.this, secondArray, getString(R.string.second), 5);
                    secondDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.second.setText(mac);
                        }
                    });
                }
                if (!secondDialog.isShowing()) {
                    secondDialog.show();
                }

            }
        });
        dataBinding.rowSystem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (systemArray == null) {
                    systemArray = getResources().getStringArray(R.array.system_array);
                }
                if (systemDialog == null) {
                    systemDialog = new SelectDialog(TimeActivity.this, systemArray, getString(R.string.hour_system));
                    systemDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.system.setText(mac);
                        }
                    });
                }
                if (!systemDialog.isShowing()) {
                    systemDialog.show();
                }

            }
        });
        dataBinding.rowArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (areaArray == null) {
                    areaArray = getResources().getStringArray(R.array.area_array);
                }
                if (areaDialog == null) {
                    areaDialog = new SelectDialog(TimeActivity.this, areaArray, getString(R.string.hour_area));
                    areaDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.area.setText(mac);

                        }
                    });
                }
                if (!areaDialog.isShowing()) {
                    areaDialog.show();
                }
            }
        });
        dataBinding.rowCurrentTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentHourArray == null) {
                    currentHourArray = getResources().getStringArray(R.array.hour_array);
                }
                if (currentHourDialog == null) {
                    currentHourDialog = new SelectDialog(TimeActivity.this, currentHourArray, getString(R.string.area_hour));
                    currentHourDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.currentHour.setText(mac);
                        }
                    });
                }
                if (!currentHourDialog.isShowing()) {
                    currentHourDialog.show();
                }
            }
        });
        dataBinding.rowCurrentMinute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentMinuteArray == null) {
                    currentMinuteArray = new String[60];
                    for (int i = 0; i < 60; i++) {
                        currentMinuteArray[i] = i + "";
                    }
                }
                if (currentMinuteDialog == null) {
                    currentMinuteDialog = new SelectDialog(TimeActivity.this, currentMinuteArray, getString(R.string.area_minute));
                    currentMinuteDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.currentMinute.setText(mac);
                        }
                    });
                }
                if (!currentMinuteDialog.isShowing()) {
                    currentMinuteDialog.show();
                }
            }
        });
        dataBinding.rowMode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (modeArray == null) {
                    modeArray = getResources().getStringArray(R.array.mode_array);
                }
                if (modeDialog == null) {
                    modeDialog = new SelectDialog(TimeActivity.this, modeArray, getString(R.string.mode));
                    modeDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.mode.setText(mac);
                        }
                    });
                }
                if (!modeDialog.isShowing()) {
                    modeDialog.show();
                }
            }
        });
        dataBinding.sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String year = dataBinding.year.getText().toString();
                String month = dataBinding.month.getText().toString();
                String day = dataBinding.day.getText().toString();
                String hour = dataBinding.hour.getText().toString();
                String minute = dataBinding.minute.getText().toString();
                String second = dataBinding.second.getText().toString();
                String system = dataBinding.system.getText().toString();
                String area = dataBinding.area.getText().toString();
                String cHour = dataBinding.currentHour.getText().toString();
                String cMinute = dataBinding.currentMinute.getText().toString();
                String mode = dataBinding.mode.getText().toString();
                if (TextUtils.isEmpty(year) || TextUtils.isEmpty(month) || TextUtils.isEmpty(day) || TextUtils.isEmpty(hour) || TextUtils.isEmpty(minute) || TextUtils.isEmpty(second)) {
                    ShowToast.show(TimeActivity.this, getString(R.string.time_hint));
                    return;
                }
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    EABleSyncTime eaBleSyncTime = new EABleSyncTime();
                    eaBleSyncTime.setYear(Integer.valueOf(year));
                    eaBleSyncTime.setMonth(Integer.valueOf(month));
                    eaBleSyncTime.setDay(Integer.valueOf(day));
                    eaBleSyncTime.setHour(Integer.valueOf(hour));
                    eaBleSyncTime.setMinute(Integer.valueOf(minute));
                    eaBleSyncTime.setSecond(Integer.valueOf(second));
                    if (!TextUtils.isEmpty(cHour)) {
                        eaBleSyncTime.setTime_zone_hour(Integer.valueOf(cHour));
                    }
                    if (!TextUtils.isEmpty(cMinute)) {
                        eaBleSyncTime.setTime_zone_minute(Integer.valueOf(cMinute));
                    }
                    if (!TextUtils.isEmpty(mode) && mode.equalsIgnoreCase(getString(R.string.mode_2))) {
                        eaBleSyncTime.setE_sync_mode(EABleSyncTime.SyncMode.watch);
                    } else {
                        eaBleSyncTime.setE_sync_mode(EABleSyncTime.SyncMode.normal);
                    }
                    if (!TextUtils.isEmpty(area)) {
                        if (area.equalsIgnoreCase(getString(R.string.area_1))) {
                            eaBleSyncTime.setE_time_zone(TimeZone.zero);
                        } else if (area.equalsIgnoreCase(getString(R.string.air_2))) {
                            eaBleSyncTime.setE_time_zone(TimeZone.east);
                        } else if (area.equalsIgnoreCase(getString(R.string.area_3))) {
                            eaBleSyncTime.setE_time_zone(TimeZone.west);
                        }
                    }
                    if (!TextUtils.isEmpty(system) && system.equalsIgnoreCase(getString(R.string.system_12))) {
                        eaBleSyncTime.setE_hour_system(EABleSyncTime.HourSystem.hour_12);
                    } else {
                        eaBleSyncTime.setE_hour_system(EABleSyncTime.HourSystem.hour_24);
                    }
                    timeData.setTime(eaBleSyncTime).observe(TimeActivity.this, new Observer<Boolean>() {
                        @Override
                        public void onChanged(Boolean aBoolean) {
                            if (aBoolean) {
                                ShowToast.show(TimeActivity.this, getString(R.string.sync_success));
                            } else {
                                ShowToast.show(TimeActivity.this, getString(R.string.sync_fail));
                            }
                        }
                    });

                }

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
            timeData.getTime().observe(TimeActivity.this, new Observer<EABleSyncTime>() {
                @Override
                public void onChanged(EABleSyncTime eaBleSyncTime) {
                    if (eaBleSyncTime != null) {
                        dataBinding.year.setText(eaBleSyncTime.getYear() + "");
                        dataBinding.month.setText(eaBleSyncTime.getMonth()+"");
                        dataBinding.day.setText(eaBleSyncTime.getDay()+"");
                        dataBinding.hour.setText(eaBleSyncTime.getHour()+"");
                        dataBinding.minute.setText(eaBleSyncTime.getMinute()+"");
                        dataBinding.second.setText(eaBleSyncTime.getSecond()+"");
                        EABleSyncTime.HourSystem hourSystem = eaBleSyncTime.getE_hour_system();
                        if (hourSystem != null && hourSystem == EABleSyncTime.HourSystem.hour_12) {
                            dataBinding.system.setText(getString(R.string.system_12));

                        } else {
                            dataBinding.system.setText(getString(R.string.system_24));
                        }
                        TimeZone timeZone = eaBleSyncTime.getE_time_zone();
                        if (timeZone != null) {
                            if (timeZone == TimeZone.zero) {
                                dataBinding.area.setText(getString(R.string.area_1));
                            } else if (timeZone == TimeZone.east) {
                                dataBinding.area.setText(getString(R.string.area_2));
                            } else if (timeZone == TimeZone.west) {
                                dataBinding.area.setText(getString(R.string.area_3));
                            }
                        }
                        dataBinding.currentHour.setText(eaBleSyncTime.getTime_zone_hour() + "");
                        dataBinding.currentMinute.setText(eaBleSyncTime.getTime_zone_minute()+"");
                        EABleSyncTime.SyncMode syncMode = eaBleSyncTime.getE_sync_mode();
                        if (syncMode != null && syncMode == EABleSyncTime.SyncMode.watch) {
                            dataBinding.mode.setText(getString(R.string.mode_2));
                        } else {
                            dataBinding.mode.setText(getString(R.string.mode_1));
                        }
                    }
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        if (yearDialog != null) {
            yearDialog.destroyDialog();
            yearDialog = null;
        }
        if (monthDialog != null) {
            monthDialog.destroyDialog();
            monthDialog = null;
        }
        if (dayDialog != null) {
            dayDialog.destroyDialog();
            dayDialog = null;
        }
        if (hourDialog != null) {
            hourDialog.destroyDialog();
            hourDialog = null;
        }
        if (minuteDialog != null) {
            minuteDialog.destroyDialog();
            minuteDialog = null;
        }
        if (secondDialog != null) {
            secondDialog.destroyDialog();
            secondDialog = null;
        }
        if (systemDialog != null) {
            systemDialog.destroyDialog();
            systemDialog = null;
        }
        if (areaDialog != null) {
            areaDialog.destroyDialog();
            areaDialog = null;
        }
        if (currentHourDialog != null) {
            currentHourDialog.destroyDialog();
            currentHourDialog = null;
        }
        if (currentMinuteDialog != null) {
            currentMinuteDialog.destroyDialog();
            currentMinuteDialog = null;
        }
        if (modeDialog != null) {
            modeDialog.destroyDialog();
            modeDialog = null;
        }
        yearArray = null;
        monthArray = null;
        dayArray = null;
        hourArray = null;
        minuteArray = null;
        secondArray = null;
        systemArray = null;
        areaArray = null;
        currentHourArray = null;
        currentMinuteArray = null;
        modeArray = null;
        timeData = null;
        if (dataBinding != null) {
            dataBinding.unbind();
            dataBinding = null;
        }
        super.onDestroy();
    }
}
